package top.lionkliu.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import top.lionkliu.entity.Note;
import top.lionkliu.mapper.NoteMapper;
import top.lionkliu.service.NoteService;
import top.lionkliu.utils.MybatisUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : lyj
 * @Timer : 2022/5/11
 * @Description :
 */
@Slf4j
public class NoteServiceImpl implements NoteService {
    @Override
    public List<Note> selectAll() {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        NoteMapper mapper = sqlSession.getMapper(NoteMapper.class);
        List<Note> notes = mapper.selectAll();
        MybatisUtil.closeSqlSession();
        return notes;
    }

    @Override
    public void update(Note note) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        NoteMapper mapper = sqlSession.getMapper(NoteMapper.class);
        mapper.update(note);
        sqlSession.commit();
        MybatisUtil.closeSqlSession();
        log.info("更新一条note记录:{}",note);
    }

    @Override
    public void delete(int id) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        NoteMapper mapper = sqlSession.getMapper(NoteMapper.class);
        Note note = mapper.selectById(id);
        mapper.delete(id);
        sqlSession.commit();
        MybatisUtil.closeSqlSession();
        log.info("删除一条note记录:{}",note);
    }

    @Override
    public void insert(Note note) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        NoteMapper mapper = sqlSession.getMapper(NoteMapper.class);
        mapper.insert(note);
        sqlSession.commit();
        MybatisUtil.closeSqlSession();
        log.info("插入一条note记录:{}",note);
    }

    @Override
    public Note selectById(int id) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        NoteMapper mapper = sqlSession.getMapper(NoteMapper.class);
        Note note = mapper.selectById(id);
        MybatisUtil.closeSqlSession();
        log.info("根据id:{}查询一条note记录:{}",id,note);
        return note;
    }

    @Override
    public List<Note> selectByLike(String key, String value) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        NoteMapper mapper = sqlSession.getMapper(NoteMapper.class);
        List<Note> notes = mapper.selectByLike(key, value);
        MybatisUtil.closeSqlSession();
        log.info("根据{}模糊查询{}，结果:{}",key,value,notes);
        return notes;
    }

    @Override
    public List<Note> selectByPage(int currPage, int pageSize) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        NoteMapper mapper = sqlSession.getMapper(NoteMapper.class);
        Map<String, Object> data = new HashMap();
        log.info("currPage:{},pageSize:{}",currPage,pageSize);
        data.put("currIndex", (currPage-1)*pageSize);
        data.put("pageSize", pageSize);
        List<Note> notes = mapper.selectByPage(data);
        MybatisUtil.closeSqlSession();
        return notes;
    }

    @Override
    public int getTotal() {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        NoteMapper mapper = sqlSession.getMapper(NoteMapper.class);
        int total = mapper.getTotal();
        log.info("查询note总条数：{}",total);
        MybatisUtil.closeSqlSession();
        return total;
    }
}
